﻿using BLL;
using BLL.Access;
using Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Class;

namespace Web.Pages.QuShui.DiXiaS.JiHuaGL.KaiCaiJH
{
    public partial class KaiCaiJHSR : BasePage
    {
        #region 属性
        /// <summary>
        /// 保存结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }
        #endregion

        #region 变量
        /// <summary>
        /// 数据访问操作类
        /// </summary>
        SpHelper _SpHelper = new SpHelper();

        /// <summary>
        /// 操作日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();
        #endregion

        #region 事件
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitData();
                BindControl();
                LoadData();
            }
            //每次回发时从内存中取得数据源重新绑定,否则列头无法排序
            this.gvList.DataSource = this.dtSource;
            this.gvList.DataBind();
        }

        /// <summary>
        /// 响应前台回调
        /// </summary>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            string msg = "";
            
            if (e.Parameter == "setinfo")
            {
                int id = Convert.ToInt32(this.hdCollection.Get("ID"));
                if (id != -1)
                {
                    int year = Convert.ToInt32(cmbYear.Value);
                    DataTable dt_temp = _SpHelper.P_QSJH_JIHUASR(4, id, year).Tables[0];
                    decimal kcjh = dt_temp.Rows[0]["I_KAICAIJH"]!= Convert.DBNull ? Convert.ToDecimal(dt_temp.Rows[0]["I_KAICAIJH"]) / 10000 : 0;
                    msg = string.Join("|", Convert.ToString(dt_temp.Rows[0]["S_YONGHUH"]), Convert.ToString(dt_temp.Rows[0]["S_YONGHUM"]),
                        Convert.ToString(kcjh));
                }
                e.Result = e.Parameter + "|0|" + msg;
            }
            else if (e.Parameter == "search")
            {
                int yhh = Convert.ToInt32(this.hdCollection.Get("yhh"));
                int year = Convert.ToInt32(cmbYear.Value);
                DataTable dt_temp = _SpHelper.P_QSJH_JIHUASR(5, yhh, year).Tables[0];
                decimal kcjh = dt_temp.Rows[0]["I_KAICAIJH"] != Convert.DBNull ? Convert.ToDecimal(dt_temp.Rows[0]["I_KAICAIJH"]) / 10000 : 0;
                msg = string.Join("|", Convert.ToString(dt_temp.Rows[0]["S_YONGHUH"]), Convert.ToString(dt_temp.Rows[0]["S_YONGHUM"]),
                    Convert.ToString(kcjh));
                e.Result = e.Parameter + "|0|" + msg;
            }
            //else if (e.Parameter == "first")
            //{
                //string ch = this.hdCollection.Get("ch").ToString();
                //DataTable dt = _spHelper.P_QSCB_CHAOBIAOSJ2(wtype, 1, CH).Tables[0];
                //if (dt.Rows.Count > 0)
                //{
                //    msg = SetText(dt, y, m, y_sy, m_sy);
                //}
            //}
            else if (e.Parameter == "first"||e.Parameter == "last" || e.Parameter == "next" || e.Parameter == "final")
            {
                int rn = Convert.ToInt32(this.hdCollection.Get("RN"));
                DataTable dt_temp = _SpHelper.P_QSJH_JIHUASR(6, rn, Convert.ToInt32(cmbYear.Value)).Tables[0];
                decimal kcjh = dt_temp.Rows[0]["I_KAICAIJH"] != Convert.DBNull ? Convert.ToDecimal(dt_temp.Rows[0]["I_KAICAIJH"]) / 10000 : 0;
                if (dt_temp.Rows.Count > 0)
                {
                    msg = string.Join("|", Convert.ToString(dt_temp.Rows[0]["S_YONGHUH"]), Convert.ToString(dt_temp.Rows[0]["S_YONGHUM"]),
                    Convert.ToString(kcjh));
                    e.Result = e.Parameter + "|0|" + msg;
                }

            }
            else if (e.Parameter == "save")
            {
                int id = this.hdCollection.Contains("ID") ? Convert.ToInt32(this.hdCollection.Get("ID")) : 0;
                QS_JH_KAICAIJHBBLL KCJHBLL = new QS_JH_KAICAIJHBBLL();
                QS_JH_KAICAIJHB KCJH = new QS_JH_KAICAIJHB();
                int y = Convert.ToInt32(cmbYear.Text);
                string yhh = txtyhh.Text != "" ? txtyhh.Text : "";
                DataTable dt_jh = KCJHBLL.SelectQS_JH_KAICAIJHBsDynamic(string.Format(" i_jlzt=0 and i_y={0} and I_TYPE=1 and I_ZIDONGCJ = 0 and S_YONGHUBH='{1}'", y, yhh), null);
                int val = Convert.ToInt32(cmUnit.Value);//1 万立方米，2 立方米
                OperateState operateState;
                if (dt_jh.Rows.Count > 0)
                {
                    id = Convert.ToInt32(dt_jh.Rows[0]["ID"]);
                    //修改操作
                    operateState = OperateState.Modify;
                    KCJH = KCJHBLL.SelectQS_JH_KAICAIJHB(id);
                    KCJH.ID = id;
                }
                //添加
                else
                {
                    operateState = OperateState.Add;
                    KCJH.ID = 0;
                    KCJH.S_YONGHUBH = yhh;
                    KCJH.I_Y = y;
                    KCJH.I_TYPE = 1;
                    KCJH.I_ZIDONGCJ = 0;
                    KCJH.S_TIANJIAR = LoginInfo.CurrentUser.UserID.ToString();
                    KCJH.D_TIANJIARQ = BusinessTool.GetDataBaseDateTime();
                }

                decimal kcjh = txtKaiCaiJH.Text != "" ? Convert.ToDecimal(txtKaiCaiJH.Text) : 0;
                KCJH.I_KAICAIJH = val == 1 ? (kcjh * 10000) : kcjh;
                KCJH.I_JLZT = 0;
                KCJH.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
                KCJH.D_CAOZUORQ = BusinessTool.GetDataBaseDateTime();
                _SYS_LOGBLL.Log<QS_JH_KAICAIJHB>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "开采计划输入", operateState, KCJH, null);
                bool result = KCJHBLL.InsertOrUpdate(KCJH);
                if (result)
                {
                    DataTable dt = _SpHelper.P_QSJH_JIHUASR(4, 0, y).Tables[0];
                    int zl = 0;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        zl += dt.Rows[i]["I_KAICAIJH"] != Convert.DBNull ? Convert.ToInt32(dt.Rows[i]["I_KAICAIJH"]) : 0;
                    }
                    msg = "true" + "|" + zl.ToString();

                    e.Result = e.Parameter + "|0|" + msg;
                }
            }
           
    }
        protected void gvList_CustomCallback(object sender, DevExpress.Web.ASPxGridViewCustomCallbackEventArgs e)
        {
            int year = Convert.ToInt32(cmbYear.Text);
            this.dtSource = _SpHelper.P_QSJH_JIHUASR(4, 0, year).Tables[0];
            this.gvList.DataSource = this.dtSource;
            this.gvList.DataBind();
        }
        #endregion

        #region 方法
        private void InitData()
        {
            int year = DateTime.Now.Year;
            //for (int i = year - 10; i <= year+1; i++)
            //{
            //    cmbYear.Items.Add(i.ToString(), i);
            //}
            //cmbYear.SelectedIndex = 11;
            DataTable dt = _SpHelper.P_QSJH_GETMAX_Y(1).Tables[0];
            int y = dt.Rows.Count > 0 ? Convert.ToInt32(dt.Rows[0][0]) : 0;
            if (y == 0)
            {
                cmbYear.Items.Add(year.ToString(), year);
            }
            else
            {
                cmbYear.Items.Add((y + 1).ToString(), y + 1);
            }
            cmbYear.SelectedIndex = 0;
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        private void LoadData()
        {
            int year = Convert.ToInt32(cmbYear.Text);
            this.dtSource = _SpHelper.P_QSJH_JIHUASR(4, 0, year).Tables[0];
            this.gvList.DataSource = this.dtSource;
            this.gvList.DataBind();
            int zl = 0;
            for (int i = 0; i < this.dtSource.Rows.Count; i++)
            {
                zl += this.dtSource.Rows[i]["I_KAICAIJH"] != Convert.DBNull ? Convert.ToInt32(this.dtSource.Rows[i]["I_KAICAIJH"]) : 0;
            }
            lbkaicai1.Text = "开采计划合计" + zl.ToString() + "立方米";
            lbzjl.Text = "共" + this.dtSource.Rows.Count.ToString() + "条";
            rows.Value = this.dtSource.Rows.Count.ToString();
            row.Value = "1";
        }

        private void BindControl()
        {
            this.cmUnit.Items.Add("万立方米", "1");//m³
            this.cmUnit.Items.Add("立方米", "2");
            this.cmUnit.SelectedIndex = 0;
        }
        #endregion

      

        protected void cmbYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadData();
        }

      

    }
}